package org.example.B_100;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Scanner;
import java.util.StringJoiner;

/**
 * @author lihao
 * @className Competition
 * @date 2023/12/14
 * @description
 * 比赛
 */
public class Competition {
    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in).useDelimiter("[,\n]");

        //输入评委数量m 和比赛选手n个
        int m = sc.nextInt();
        int n = sc.nextInt();

        int[][] scores = new int[m][n];

        //获取评委得分
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                scores[i][j] = sc.nextInt();
            }
        }

        //经过计算获取结果
        System.out.println(getResult(m,n,scores));

    }

    //对每一组数据进行排序并且获取总得分映射到一个map中，最后再排序，取得前三名
    public static String getResult(int m,int n,int[][] scores){
        //输入校验
        if (m<3 || m > 10 || n < 3 || m > 100){
            return "-1";
        }

        //定义运动员对象
        HashMap<Integer,Integer[]> players = new HashMap<>();

        for (int j = 0; j < n; j++) {
            Integer[] player = new Integer[m];
            for (int i = 0; i < m; i++) {
                //判断队员得分是否合法
                if (scores[i][j] > 10 || scores[i][j] < 1) return "-1";
                player[i] = scores[i][j];
            }
            //按照队员得分降序
            Arrays.sort(player,(a,b) -> b-a);
            players.put(j,player);
        }

        StringJoiner sj = new StringJoiner(",");

        //对map进行排序
        players.entrySet().stream()
                .sorted(
                        (a,b)->{
                            Integer[] playerA = a.getValue();
                            Integer[] playerB = b.getValue();

                            int sumA = sum(playerA);
                            int sumB = sum(playerB);

                            if (sumA != sumB){
                                return sumB - sumA;
                            }
                            
                            //若两组数据求和相等，则判断谁的大值多
                            for (int i = 0; i < m; i++) {
                                if (playerA[i] == playerB[i]) continue;
                                return playerB[i] - playerA[i]; //这里没理解
                            }

                            return 0 ;
                        })
                .limit(3)
                .forEach(p -> sj.add(p.getKey() + 1 + ""));

        return sj.toString();
    }

    public static int sum(Integer[] arr){
        return Arrays.stream(arr).reduce(Integer::sum).orElse(0);
    }
}
